#include "autoplaytext.hpp"

void AutoPlayText::SetCallback(void(*f)(void*),void* a){
    arg=a,callback=f;
}
unsigned char AutoPlayText::Alpha(){
    unsigned char a;
    SDL_GetTextureAlphaMod(back,&a);
    return a;
}
void AutoPlayText::SetAlpha(unsigned char a){
    SDL_SetTextureAlphaMod(back,a);
    if(_t)SDL_SetTextureAlphaMod(_t,a);
}
void AutoPlayText::AutoPlay(void* g){
    auto a=(AutoPlayText*)g;
    if(a->pos==a->cache.end()||!a->_enabled)return;
    auto& i=a->pos;
    auto& r=i->rect;
    if(r.w<i->w){
        r.w+=4;
        return;
    }
    r.w=i->w;
    a->pos++;
}
void AutoPlayText::AutoPlayFast(void* g){
    auto a=(AutoPlayText*)g;
    if(a->pos==a->cache.end()||!a->_enabled)return;
    auto& i=a->pos;
    auto& r=i->rect;
    if(r.w<i->w){
        r.w+=8;
        return;
    }
    r.w=i->w;
    a->pos++;
}
bool AutoPlayText::IsFast(){return is_fast;}
void AutoPlayText::Play(){
    is_fast=false;
    if(task)_belong->RemoveTask(task);
    task=_belong->AddTask(AutoPlay,this,1,true,true);
}
void AutoPlayText::PlayFast(){
    is_fast=true;
    if(task)_belong->RemoveTask(task);
    task=_belong->AddTask(AutoPlayFast,this,1,true,true);
}
bool AutoPlayText::Finish(){
    return pos==cache.end();
}
void AutoPlayText::Stop(){
    _belong->RemoveTask(task);
}
void AutoPlayText::SetText(const vector<TextMod>& text){
    _cache=text;
    if(_t){
        SDL_DestroyTexture(_t);
        cache.clear();
    }
    vector<pair<int,int>> a;
    _t=BuildText(text,default_font->_font,a);
    cache.resize(a.size());
    auto begin=cache.begin();
    int y=0;
    for(auto& n:a){
        begin->w=n.first,begin->h=n.second;
        begin->rect={0,y,0,n.second};
        y+=n.second;
        begin++;
    }
    pos=cache.begin();
}
void AutoPlayText::SetText(const initializer_list<TextMod>& text){
    _cache=text;
    if(_t){
        SDL_DestroyTexture(_t);
        cache.clear();
    }
    vector<pair<int,int>> a;
    _t=BuildText(text,default_font->_font,a);
    cache.resize(a.size());
    auto begin=cache.begin();
    int y=0;
    for(auto& n:a){
        begin->w=n.first,begin->h=n.second;
        begin->rect={0,y,0,n.second};
        y+=n.second;
        begin++;
    }
    pos=cache.begin();
}
AutoPlayText::AutoPlayText(Window* win,SDL_Rect& r,const char* back_path,void(*callback)(void*),void* arg):
Control(win),_t(0),task(0),callback(callback),arg(arg){
    back=win->renderer->LoadTexture(back_path);
    SDL_SetTextureBlendMode(back,SDL_BLENDMODE_BLEND);
    SDL_SetTextureAlphaMod(back,0);
    _area=r;
    is_fast=false;
}
AutoPlayText::~AutoPlayText(){
    if(_t)SDL_DestroyTexture(_t);
    SDL_DestroyTexture(back);
}
SDL_Texture* AutoPlayText::Background(){return back;}
void AutoPlayText::_draw(){
    auto r=_belong->renderer;
    r->DrawTexture(0,&_area,back);
    auto t=_t;
    auto x=_area.x+120,y=_area.y+50;
    for(auto& n:cache){
        auto& _r=n.rect;
        SDL_Rect dst={x,y+_r.y,_r.w,_r.h};
        r->DrawTexture(&_r,&dst,t);
    }
}
void AutoPlayText::_release(int x,int y,int clicks,unsigned char key){
    if(key==SDL_BUTTON_RIGHT){
        auto end=cache.end();
        while(pos!=end){
            pos->rect.w=pos->w;
            pos++;
        }
        return;
    }
    callback(arg);
}